package com.zhc.dejingapi.job;

import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.qingflow.appfield.QingFieldResultMainBase;
import com.zhc.dejingapi.service.upload.IQingAppFieldService;
import com.zhc.dejingapi.service.upload.IQingAppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Slf4j
@Configuration
public class QingflowInfoSynJob {
    private  static  boolean isRunning=false;
    private final JdbcTemplate mysqlJdbcTemplate;
    private final IQingAppInfoService qqingAppInfoService;
    private final IQingAppFieldService qqingAppFieldService;

    public QingflowInfoSynJob(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                              IQingAppInfoService qqingAppInfoService1,
                              IQingAppFieldService qqingAppFieldService1) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.qqingAppInfoService = qqingAppInfoService1;
        this.qqingAppFieldService = qqingAppFieldService1;
    }

    public  void executeJob(){

        if(isRunning){
            log.info("ERPProdClassToQingJob is running");
            return;
        }

        isRunning=true;
        int i=0;
        if(i==1){
            try{
                String strJSON="{\n" +
                        "    \"errCode\": 49303,\n" +
                        "    \"errMsg\": \"\",\n" +
                        "    \"questionRelations\": [],\n" +
                        "    \"result\": null\n" +
                        "}";
                //QingFieldResultMainBase mainBase= JSON.parseObject(strJSON, QingFieldResultMainBase.class);
                Gson gson = new Gson();
                QingFieldResultMainBase mainBase=gson.fromJson(strJSON,QingFieldResultMainBase.class);

                log.info(mainBase.getErrMsg());
            }
            catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage()+"\r\n"+e.getStackTrace());
            }

            try {
                String strSql="select Count(*) from qing_appinfo";
                Integer iCount=mysqlJdbcTemplate.queryForObject(strSql,Integer.class);
                log.info("ERPProdClassToQingJob is running,count:"+iCount);
            }
            catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage()+"\r\n"+e.getStackTrace());
            }

            try{

                qqingAppInfoService.SynAppInfo();


            }
            catch (Exception e){
                log.error("ERPProdClassToQingJob error",e);
            }

            try{

                qqingAppFieldService.SynAppField();

            }
            catch (Exception e){
                e.printStackTrace();
                log.error("ERPProdClassToQingJob error",e);
            }
        }

        isRunning=false;
    }

}
